package com.springboot.eduservice.service.impl;

import com.springboot.eduservice.entity.EduCourse;
import com.springboot.eduservice.entity.EduCourseDescription;
import com.springboot.eduservice.entity.vo.CourseInfoVo;
import com.springboot.eduservice.entity.vo.CoursePublishVo;
import com.springboot.eduservice.mapper.EduCourseMapper;
import com.springboot.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-05-12
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduCourseDescriptionServiceImpl eduCourseDescriptionService;

    public String addCourseDescription(CourseInfoVo courseInfoVo){

        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,course);
        int insert = baseMapper.insert(course);
        if (insert == 0){
            //添加失败,全局异常处理
            throw new GuliException(20001,"添加课程信息失败");
        }
        String id = course.getId();
        //课程简介
        String description = courseInfoVo.getDescription();
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setId(id);
        courseDescription.setDescription(description);
        eduCourseDescriptionService.save(courseDescription);
        return id;
    }

    public CourseInfoVo getCourseByIId(String id){
        EduCourse course = baseMapper.selectById(id);
        EduCourseDescription byId = eduCourseDescriptionService.getById(id);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        courseInfoVo.setDescription(byId.getDescription());
        BeanUtils.copyProperties(course,courseInfoVo);
        return  courseInfoVo;
    }

    public void updateCourse(CourseInfoVo courseInfoVo){
        //修改课程表
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,course);
        System.out.println(courseInfoVo);
        int insert = baseMapper.updateById(course);
        if(insert == 0) {
            throw new GuliException(20001,"课程信息修改失败");
        }
        //修改课程描述表
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(courseInfoVo.getId());
        eduCourseDescriptionService.updateById(eduCourseDescription);

    }
    //根据课程id查询课程确认信息
    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        //调用mapper
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }



}
